package com.zl.freight.ui.activity;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.RegexUtils;
import com.zl.freight.R;
import com.zl.freight.base.BaseActivity;
import com.zl.freight.mode.CarInformationEntity;
import com.zl.freight.mode.TopNewsBean;
import com.zl.freight.utils.API;
import com.zl.freight.utils.SoapCallback;
import com.zl.freight.utils.SoapUtils;
import com.zl.freight.utils.SpUtils;
import com.zl.zlibrary.utils.GsonUtils;
import com.zl.zlibrary.utils.ImageFactory;

import org.json.JSONArray;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zhanglei
 * @date 17/12/30
 * 发布广告页
 */
public class ADActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.et_title_ad)
    EditText etTitleAd;
    @BindView(R.id.et_ad_content)
    EditText etAdContent;
    @BindView(R.id.et_ad_url)
    EditText etAdUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ad);
        ButterKnife.bind(this);
        initView();
        getNews();
    }


    /**
     * 获取管理员发布的广告
     */
    private void getNews() {
        SoapUtils.Post(mActivity, API.GetAdminInfo, null, new SoapCallback() {
            @Override
            public void onError(String error) {
                showToast(error);
            }

            @Override
            public void onSuccess(String data) {
                try {
                    JSONArray array = new JSONArray(data);
                    if (array.length() < 0) {
                        showToast("还没有广告，请先发布一条");
                        return;
                    }
                    TopNewsBean carInformationEntity = GsonUtils.fromJson(array.optString(0), TopNewsBean.class);
                    etTitleAd.setText(carInformationEntity.getInfoTitle());
                    etAdContent.setText(carInformationEntity.getInfoContent());
                    etAdUrl.setText(carInformationEntity.getInfoLink());
                } catch (Exception e) {
                    Log.e("body","");
                }
            }
        });
    }

    private void initView() {
        tvTitle.setText("发布广告");
        tvTitleRight.setText("修改并发布");
    }

    @OnClick({R.id.iv_back, R.id.tv_title_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_title_right:
                commit();
                break;
        }
    }

    /**
     * 发布广告
     */
    private void commit() {
        String title = etTitleAd.getText().toString().trim();
        String content = etAdContent.getText().toString().trim();
        String url = etAdUrl.getText().toString().trim();

        if (TextUtils.isEmpty(title)) {
            showToast("文章标题不能为空");
            return;
        }
        if (TextUtils.isEmpty(content)) {
            showToast("文章内容不能为空");
            return;
        }
        if (TextUtils.isEmpty(url)) {
            showToast("网址不能为空");
            return;
        }
        if (!RegexUtils.isURL(url)) {
            showToast("请输入正确的网址");
            return;
        }

        CarInformationEntity entity = new CarInformationEntity();
        entity.setInfoContent(content);
        entity.setInfoTitle(title);
        entity.setUserId(SpUtils.getUserData(mActivity).getId());
        entity.setInfoLink(url);
        showDialog("文章发布中...");
        Map<String, String> params = new HashMap<>();
        params.put("InfoJson", GsonUtils.toJson(entity));
        SoapUtils.Post(mActivity, API.AddInfo, params, new SoapCallback() {
            @Override
            public void onError(String error) {
                hideDialog();
                showToast(error);
            }

            @Override
            public void onSuccess(String data) {
                hideDialog();
                showToast("发布成功");
                finish();
            }
        });
    }
}
